package com.xigua.xcar.search;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xigua.xcar.R;
import com.xigua.xcar.RoutePlan;
import com.xigua.xcar.base.BaseActivity;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.shoplist.ShopListActivity;
import com.xigua.xcar.util.CommonListUtil;

public class SearchActivity extends BaseActivity {
	private String tag = "SearchActivity";
	private EditText mEdtInput = null;
	@Override
	protected void finalize() throws Throwable {
		// 注销的注意事项
		super.finalize();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "onCreate");
		super.onCreate(savedInstanceState);
		setView(R.layout.search);
		setTopBarTitle("搜索-杭州");
		setTitleBar(0, "", 0, "");
		mEdtInput = (EditText) this.findViewById(R.id.edit_search_input);
		Button btn = (Button) this.findViewById(R.id.btn_search_find);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				jumpToShopListActivity();
			}
		});
		btn = (Button) this.findViewById(R.id.btn_search_clear);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mEdtInput.setText("");
			}
		});
		btn = (Button) this.findViewById(R.id.btn_search_go);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				jumpToShopListActivity();
			}
		});
	}

	private boolean isGetLocation(){
		if(null == RoutePlan.getLocation() || (0 == RoutePlan.getLocation().getLatitude() || 0 == RoutePlan.getLocation().getLongitude())){
			return false;
		}else{
			return true;
		}
	}
	
	private static final String KEY_FLAG = "flag";
	
	private boolean getFlagFromSp(){
		boolean ret = false;
		SharedPreferences sp = this.getPreferences(MODE_WORLD_READABLE);
		ret = sp.getBoolean(KEY_FLAG, false);
		return ret;
	}
	
	private void saveFlagToSp(){
		SharedPreferences sp = this.getPreferences(MODE_WORLD_WRITEABLE);
		Editor edit = sp.edit();
		edit.putBoolean(KEY_FLAG, true);
		edit.commit();
	}
	
	private void jumpToShopListActivity() {
		Editable txt = mEdtInput.getEditableText();
		if ("".equals(txt.toString())) {
			Toast.makeText(this, "您输入的关键词为空", 3000).show();
		} else {
			if(isGetLocation()){
				CommonListUtil.saveLocationPointToSP(this, (int)(RoutePlan.getLocation().getLongitude()*1e6), (int)(RoutePlan.getLocation().getLatitude()*1e6));
			}else{
				CommonListUtil.saveLocationPointToSP(this, 0, 0);
			}
			if(!getFlagFromSp()){
				saveFlagToSp();
				Toast.makeText(this, "迈吉卡目前只支持杭州地区，其他地区敬请期待", 3000).show();
			}
			Intent intent = new Intent(SearchActivity.this,
					ShopListActivity.class);
			intent.putExtra(Constants.REQUEST_TYPE, 1);
			intent.putExtra(Constants.REQUEST_KEYWORD, txt.toString());
			intent.putExtra(Constants.REQUEST_ISNEARBY, 0);
			this.startActivity(intent);
		}
	}

	@Override
	protected void handleTitleBarEvent(int buttonId) {
		switch (buttonId) {
		case Constants.LEFTBUTTONID: {
			break;
		}
		case Constants.RIGHTBUTTONID: {
			break;
		}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(tag, "back");
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
	    	 AlertDialog.Builder builder = new AlertDialog.Builder(this);  
	    	    builder.setTitle("提示");  
	    	    builder.setMessage("确定要退出程序吗?");  
	    	    builder.setPositiveButton("确定",  
	    	            new DialogInterface.OnClickListener() {  
	    	                public void onClick(DialogInterface dialog, int whichButton) {           
	    	                	SearchActivity.this.finish();  
	    	                }  
	    	            });  
	    	    builder.setNegativeButton("取消",  
	    	            new DialogInterface.OnClickListener() {  
	    	                public void onClick(DialogInterface dialog, int whichButton) {  
	    	                }  
	    	            }).create(); 
	    	
	    	    builder.show();
	    	    
	        return true;
	    }
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onPause() {
    	Log.i(tag, "onPause");
//    	if(false == mIsDevelop){
//    		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//    		// 移除listener
//    		app.mBMapMan.stop();
//    	}
		super.onPause();
	}
    
	@Override
	protected void onResume() {
		Log.i(tag, "onResume");
//		if(false == mIsDevelop){
//			BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//			// 注册Listener
//	        app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
//			app.mBMapMan.start();
//		}
//		myHander.sendEmptyMessage(0);
//		myHander.sendMessageDelayed(myHander.obtainMessage(0), 10000);
//		BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication();
//		// 移除listener
//		app.mBMapMan.start();
		super.onResume();
	}
}
